<?php
class Business_News
{
	protected $_tableName = "news";

	public function getByCateId($cateId, $page = 1, $rowCount = 10, $order = "pubtime desc")
	{
		$fields = array("id", "title", "pubtime");
		$select = Db_Factory::getInstance()->createSelect();
		$select->from($this->_tableName, $fields)
				->where('cateid = ?', $cateId)
				->order($order)
				->limitPage($page, $rowCount);
		$stmt = $select->query();
		$result = $stmt->fetchAll();
		return $result;
	}
	
	/**
	 * 获得某分类下新闻总数
	 * @param Integer $cateId
	 * @return Integer
	 */
	public function getByCateIdAmount($cateId)
	{
	    $select = Db_Factory::getInstance()->createSelect();
	    $select->from($this->_tableName, "count(*) as count")
	           ->where('cateid = ?', $cateId);
	    $stmt = $select->query();
	    $result = $stmt->fetch();
	    $count = isset($result['count']) ? $result['count'] : 0;
	    return $count; 
	}
	
	/**
	 * 根据ID获取产品
	 * @param Integer $id
	 * @return Array
	 */
	public function getById($id)
	{
	    $fields = array('id', 'title', 'content', 'pubtime', 'cateid');
	    $select = Db_Factory::getInstance()->createSelect();
	    $select->from($this->_tableName, $fields)
	            ->where("id = ?", $id);
	    return $select->query()->fetch();
	}
	
	/**
	 * 添加记录
	 * @param Array $data
	 * @return Ambigous <mixed, multitype:>
	 */
	public function add($data)
	{
		$data['ctime'] = $data['mtime'] = date("Y-m-d H:i:s");
		$table = Db_Factory::getInstance()->createTable($this->_tableName);
		return $table->insert($data);
	}
	
	/**
	 * 更新记录
	 * @param Array $data
	 * @param Integer $pid
	 * @return number
	 */
	public function update($data, $id)
	{
		$data['mtime'] = date("Y-m-d H:i:s");
		$where = array("id = ?" => $id);
		$table = Db_Factory::getInstance()->createTable($this->_tableName);
		return $table->update($data, $where);
	}
	
	/**
	 * 删除记录
	 * @param Integer $id
	 * @return number
	 */
	public function delete($id)
	{
		$id = intval($id);
		$where = array("id = ?" => $id);
		$table = Db_Factory::getInstance()->createTable($this->_tableName);
		return $table->delete($where);
	}
}